package com.qf.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.test.pojo.DtsCategory;
import com.qf.test.service.CategoryService;
import com.qf.test.util.ResponseUtil;
import com.qf.test.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品类目
 */

//接口地址 : http://localhost:8083/admin/category/list?page=1&limit=20&sort=add_time&order=desc
//请求网址: http://127.0.0.1:8083/admin/category/list?page=1&limit=20&id=aaa&name=sss&sort=add_time&order=desc
@RestController

//解决前后端分离问题
@CrossOrigin("*")
@RequestMapping("/category")
public class Categorycontroller {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/list")
    public Object list(
            @RequestParam(defaultValue ="1" ) Integer page,
            @RequestParam(defaultValue ="20" ) Integer limit,
            String id, String name,
            @RequestParam(defaultValue ="add_time" )  String add_time,
            @RequestParam(defaultValue ="desc" ) String order){
    //查询 并返回分页结果
        IPage<DtsCategory> iPage = categoryService.querycategory(page, limit, id, name, add_time, order);
        //获取结果集
        List<DtsCategory> records = iPage.getRecords();
        //获得总条数
        long count = iPage.getTotal();

        //封装对象返回
       Map<String, Object> data = new HashMap<>();
       data.put("total",count);
       data.put("items",records);

        return ResponseUtil.ok(data);
    }
    /**
     * 请求网址: http://127.0.0.1:8083/admin/category/create
     * 添加商品
     */
    @PostMapping("/create")
    public Object addcreate(@RequestBody DtsCategory dtsCategory) {
        DtsCategory data = categoryService.add(dtsCategory);

        return ResponseUtil.ok(data);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Object update(@RequestBody DtsCategory dtsCategory){
        int count = categoryService.upadate(dtsCategory);
        return ResponseUtil.ok(count);
    }

    /**
     * 删除
     * @param dtsCategory
     * @return
     */
    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsCategory dtsCategory){
        int count = categoryService.upadate(dtsCategory);
        return ResponseUtil.ok(count);
    }

    /**
     * 类目
     * @return
     */
    @RequestMapping("/l1")
    public Object l1( ){

        List<CatVo> data = categoryService.select();
        return ResponseUtil.ok(data);
    }


}

